# functions to research the behaviour of Gaps
# using the table va_stratlab.RESEARCH_GAPS

CandleColorAfterGap <- function(groupcode="ALL", direction="up") {
  con <- connectToVadb()
  sqlcmd <- function(groupcode, direction){
    if (direction=="up") {
      dir <- ">0"
    } else {
      dir <- "<0"
    }
    sql <- sprintf("SELECT * FROM `RESEARCH_GAPS` WHERE `direction` %s", dir)    
    if (groupcode=="ALL") {
      return(sql)
    } else {
      sql <- paste(sql, sprintf("AND `group` = '%s'", groupcode))
      return (sql)
    }
  }
  df <- dbGetQuery(con, sqlcmd(groupcode, direction))
  dfWhiteC <- subset(df, CColor>0)
  dfBlackC <- subset(df, CColor<0)
  dfNeutralC <- subset(df, CColor==0)
  cat ("found", nrow(df), direction, "gaps.\n")
  cat (nrow(dfWhiteC),"(", round(nrow(dfWhiteC)*100/nrow(df), 2) ,"% ) candles after the gap are white\n")
  cat (nrow(dfBlackC),"(", round(nrow(dfBlackC)*100/nrow(df),2 ) ,"% ) candles after the gap are black\n")
  cat (nrow(dfNeutralC),"(", round(nrow(dfNeutralC)*100/nrow(df),2 ) ,"% ) candles after the gap are neutral\n")
  disconnectFromVadb(con)
  return (df)
}
